package com.leetcode.根据算法进行分类.位运算相关;

/**
 * @author: ZhouBert
 * @date: 2021/3/5
 * @description: 剑指 Offer 65. 不用加减乘除做加法
 * https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/
 */
public class A_剑指Offer65_不用加减乘除做加法 {

	static A_剑指Offer65_不用加减乘除做加法 action = new A_剑指Offer65_不用加减乘除做加法();

	public static void main(String[] args) {
		test1();
	}


	public static void test1() {
		int a = 5;
		int b = 5;
		int res = action.add(a, b);
		System.out.println("res = " + res);
	}

	public int add(int a, int b) {
		while (b != 0) { // 当进位为 0 时跳出
			int c = (a & b) << 1;  // c = 进位
			a ^= b; // a = 非进位和
			b = c; // b = 进位
		}
		return a;
	}
}
